package com.ruoyi.web.controller.school;

import com.ruoyi.common.constant.ConfigConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.student.domain.SchoolDegreeInfo;
import com.ruoyi.student.domain.vo.DepartmentListVo;
import com.ruoyi.student.domain.vo.GradeListVo;
import com.ruoyi.student.domain.vo.MajorListVo;
import com.ruoyi.student.domain.vo.SelectListVo;
import com.ruoyi.student.service.ISchoolDegreeInfoService;
import com.ruoyi.student.service.IStudentService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/school/degree")
public class DegreeController extends BaseController {
    @Resource
    private ISchoolDegreeInfoService schoolDegreeInfoService;


    @Resource
    private IStudentService studentService;

    @Resource
    private ISysDictTypeService dictTypeService;

    @Resource
    private ISysConfigService sysConfigService;


    /**
     * 查询学籍信息管理列表
     */
    @GetMapping("/list")
    public TableDataInfo schoolGradeInfoList(SchoolDegreeInfo schoolGradeInfo) {
        startPage();
        List<SchoolDegreeInfo> list = schoolDegreeInfoService.selectSchoolGradeInfoList(schoolGradeInfo);
        return getDataTable(list);
    }

    /**
     * 导出学籍信息管理列表
     */
    @PostMapping("/export")
    public void schoolGradeInfoExport(HttpServletResponse response, SchoolDegreeInfo schoolGradeInfo) {
        List<SchoolDegreeInfo> list = schoolDegreeInfoService.selectSchoolGradeInfoList(schoolGradeInfo);
        ExcelUtil<SchoolDegreeInfo> util = new ExcelUtil<SchoolDegreeInfo>(SchoolDegreeInfo.class);
        util.exportExcel(response, list, "数据");
    }


    /**
     * 获取学籍信息管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult schoolGradeInfoGetInfo(@PathVariable("id") Long id) {
        return success(schoolDegreeInfoService.selectSchoolGradeInfoById(id));
    }


    @PostMapping
    public AjaxResult schoolGradeInfoAdd(@RequestBody SchoolDegreeInfo schoolGradeInfo) {
        return toAjax(schoolDegreeInfoService.insertSchoolGradeInfo(schoolGradeInfo));
    }


    /**
     * 修改学籍信息管理
     */
    @PutMapping
    public AjaxResult schoolGradeInfoEdit(@RequestBody SchoolDegreeInfo schoolGradeInfo) {
        return toAjax(schoolDegreeInfoService.updateSchoolGradeInfo(schoolGradeInfo));
    }

    /**
     * 删除学籍信息管理
     */
    @DeleteMapping("/{ids}")
    public AjaxResult schoolGradeInfoRemove(@PathVariable Long[] ids) {
        return toAjax(schoolDegreeInfoService.deleteSchoolGradeInfoByIds(ids));
    }


    /**
     * 初始化年级、系别、专业
     *
     * @return
     */
    @GetMapping(value = "/init")
    public AjaxResult init() {
        //清空原有表数据
        schoolDegreeInfoService.deleteAll();
        //获取年级、系别、专业聚合
        List<SchoolDegreeInfo> query = studentService.selectAggrSchoolDegreeInfoList();
        for (SchoolDegreeInfo info : query) {
            schoolDegreeInfoService.insertSchoolGradeInfo(info);
        }
        return success("操作成功！");
    }


    /**
     * 获取年级的selectlist列表
     *
     * @return
     */
    @GetMapping(value = "/grade/selectlist")
    public AjaxResult getGradeList() {
        List<GradeListVo> query = schoolDegreeInfoService.selectList();
        List<SelectListVo> list = new ArrayList<>();
        for (GradeListVo vo : query) {
            SelectListVo dto = new SelectListVo();
            dto.setLabel(vo.getGrade().toString());
            dto.setValue(vo.getGrade().toString());
            list.add(dto);
        }
        return success(list);
    }


    /**
     * 获取毕业年份列表
     *
     * @return
     */
    @GetMapping(value = "/graduationYear/selectlist")
    public AjaxResult getGraduationYearList() {
        String listStr = sysConfigService.selectConfigByKey(ConfigConstants.GRADUATION_YEAR_LIST);
        List<SelectListVo> list = new ArrayList<>();
        if (!StringUtils.isBlank(listStr)) {
            for (String item : listStr.split(",")) {
                SelectListVo dto = new SelectListVo();
                dto.setLabel(item);
                dto.setValue(item);
                list.add(dto);
            }
        }
        return success(list);
    }


    /**
     * 获取系别列表
     *
     * @return
     */
    @GetMapping(value = "/department/selectlist")
    public AjaxResult getDepartmentList() {
        List<DepartmentListVo> query = schoolDegreeInfoService.selectDepartmentGroup();
        List<SelectListVo> list = new ArrayList<>();
        for (DepartmentListVo vo : query) {
            SelectListVo dto = new SelectListVo();
            dto.setLabel(vo.getDepartment());
            dto.setValue(vo.getDepartment());
            list.add(dto);
        }
        return success(list);
    }

    /**
     * 获取专业列表
     *
     * @return
     */
    @GetMapping(value = "/major/selectlist")
    public AjaxResult getMajorList() {
        List<MajorListVo> query = schoolDegreeInfoService.selectMajorGroup();
        List<SelectListVo> list = new ArrayList<>();
        for (MajorListVo vo : query) {
            SelectListVo dto = new SelectListVo();
            dto.setLabel(vo.getMajor());
            dto.setValue(vo.getMajor());
            list.add(dto);
        }
        return success(list);
    }


    /**
     * 获取学制下拉列表
     *
     * @return
     */
//    @GetMapping("/studystate/selectlist")
//    public AjaxResult getStudyState() {
//        List<SysDictData> studyStateList = dictTypeService.selectDictDataByType("study_state");
//        List<SelectListVo> list = new ArrayList<>();
//        for (SysDictData data : studyStateList) {
//            SelectListVo dto = new SelectListVo();
//            dto.setLabel(data.getDictLabel());
//            dto.setValue(data.getDictValue());
//            list.add(dto);
//        }
//        return success(list);
//    }


}
